<template>
  <el-container class="homeContainer">
    <!-- 头部区域 -->
    <el-header>
      <div class="logoContainer">
        <img src="../../assets/logo.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="unlogin">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏菜单区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggleButton" @click="isCollapse = !isCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse='isCollapse'
          :collapse-transition='false'
          router
          :default-active="activePath"
        >
          <el-submenu v-for="item in menuList" :index="item.id + ''" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="childItem in item.children" :index="'/' + childItem.path" :key="childItem.id" @click="saveActivePath('/' + childItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{childItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
      return {
          menuList: [],
          iconObj: {
              '125': 'iconfont icon-users',
              '103': 'iconfont icon-tijikongjian',
              '101': 'iconfont icon-shangpin',
              '102': 'iconfont icon-danju',
              '145': 'iconfont icon-baobiao'
          },
          isCollapse: false,
          // 提供路径给default-active实现菜单的高亮显示
          activePath: ''
      }
  },
  methods: {
    unlogin() {
      // 退出登录就是清空token，然后跳转到登录界面
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
       const {data: res} = await this.$http.get('menus')
       if(res.meta.status !== 200) return this.$message.error('获取菜单列表失败！！！')
       this.menuList = res.data
    },
    saveActivePath(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    } 
  },
  created() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
  },
}
</script>

<style lang="less" scoped>
.homeContainer {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .logoContainer {
    display: flex;
    align-items: center;
    span {
      font-size: 25px;
      color: cornsilk;
      margin-left: 20px;
    }
    img {
      width: 30px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu{
      border-right: none;
  }
}

.el-main {
  background-color: #ddd;
}

.iconfont {
    margin-right: 10px;
}

.toggleButton{
    background-color: rgb(87, 103, 128);
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    color: #fff;
    cursor: pointer;
}

</style>